﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Reviewer.Entities;
using Reviewer.Entities.Services;
using Reviewer.Business;
using System.Collections.Generic;

public partial class public_html_NewSiteTest_Releaseliste : System.Web.UI.Page
{
    private delegate ICollection<ReviewSearchResult> ReviewSearchEventHandler(string searchFor);
    private delegate ICollection<News> NewsSearchEventHandler(string searchFor);
    private delegate ICollection<Game> GameSearchEventHandler(string searchFor);

    private ReviewSearchEventHandler reviewSearch;
    private NewsSearchEventHandler newsSearch;
    private GameSearchEventHandler gameSearch;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string searchWord = Request["Ord"];

            RegisterAsyncTask(new PageAsyncTask(
                delegate(object obj, EventArgs eventArgs, AsyncCallback callback, object state)
                {
                    this.reviewSearch = new ReviewSearchEventHandler(this.SearchReviews);
                    return this.reviewSearch.BeginInvoke(searchWord, callback, state);
                },
                delegate(IAsyncResult result)
                {
                    ICollection<ReviewSearchResult> items = this.reviewSearch.EndInvoke(result);
                    if (items == null || items.Count == 0)
                    {
                        this.uiPlhReviews.Visible = false;
                    }
                    else
                    {
                        this.uiRepReviews.DataSource = items;
                        this.uiRepReviews.DataBind();
                        this.uiLitReviewCount.Text = items.Count.ToString();
                    }
                    this.reviewSearch = null;
                },
                delegate(IAsyncResult result)
                {
                    this.reviewSearch = null;
                    this.uiPlhReviews.Visible = false;
                },
                null, true));

            RegisterAsyncTask(new PageAsyncTask(
                 delegate(object obj, EventArgs eventArgs, AsyncCallback callback, object state)
                 {
                     this.newsSearch = new NewsSearchEventHandler(this.SearchNews);
                     return this.newsSearch.BeginInvoke(searchWord, callback, state);
                 },
                delegate(IAsyncResult result)
                {
                    ICollection<News> items = this.newsSearch.EndInvoke(result);
                    if (items == null || items.Count == 0)
                    {
                        this.uiPlnNews.Visible = false;
                    }
                    else
                    {
                        this.uiRepNews.DataSource = items;
                        this.uiRepNews.DataBind();
                        this.uiLitNewsCount.Text = items.Count.ToString();
                    }
                    this.newsSearch = null;
                },
                delegate(IAsyncResult result)
                {
                    this.newsSearch = null;
                    this.uiPlnNews.Visible = false;
                },
                null, true));

            RegisterAsyncTask(new PageAsyncTask(
                 delegate(object obj, EventArgs eventArgs, AsyncCallback callback, object state)
                 {
                     this.gameSearch = new GameSearchEventHandler(this.SearchGames);
                     return this.gameSearch.BeginInvoke(searchWord, callback, state);
                 },
                delegate(IAsyncResult result)
                {
                    ICollection<Game> items = this.gameSearch.EndInvoke(result);
                    if (items == null || items.Count == 0)
                    {
                        this.uiPlhGames.Visible = false;
                    }
                    else
                    {
                        this.uiRepGames.DataSource = items;
                        this.uiRepGames.DataBind();
                        this.uiLitGameCount.Text = items.Count.ToString();
                    }
                    this.gameSearch = null;
                },
                delegate(IAsyncResult result)
                {
                    this.gameSearch = null;
                    this.uiPlhGames.Visible = false;
                },
                null, true));

            lblOverskrift.Text = "Søge Resultater på '" + searchWord + "'";
            this.Title = "Søgning på " + searchWord;
        }
    }

    private ICollection<Game> SearchGames(string searchFor)
    {
        using (IGameService service = ServiceFactory.GameService)
        {
            return service.Search("%" + searchFor + "%", null, 50);
        }
    }

    private ICollection<ReviewSearchResult> SearchReviews(string searchFor)
    {
        using (IReviewService service = ServiceFactory.ReviewService)
        {
            return service.Search("%" + searchFor + "%", null, null, null, null, 50);
        }
    }

    private ICollection<News> SearchNews(string searchFor)
    {
        using (INewsService service = ServiceFactory.NewsService)
        {
            return service.Search("%" + searchFor + "%");
        }
    }

    public string GetFormatName(object id)
    {
        using (IMetadataService<Format> service = ServiceFactory.GetMetadataService<Format>())
        {
            Format item = service.GetAll().FirstOrDefault(t => t.Id.Equals(Convert.ToInt32(id)));
            if (item != null)
            {
                return item.Name;
            }
        }

        return "Ukendt format";
    }

    public string GetGenreName(object id)
    {
        using (IMetadataService<Genre> service = ServiceFactory.GetMetadataService<Genre>())
        {
            Genre item = service.GetAll().FirstOrDefault(t => t.Id.Equals(Convert.ToInt32(id)));
            if (item != null)
            {
                return item.Name;
            }
        }

        return "Ukendt genre";
    }

}
